﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKArchUtils.Utils;

namespace YJKRevitRampModule.CreateRampByDoor
{
	public class RampFamilyCreator
	{
		public RampFamilyCreator(ExternalCommandData cmdData, ElementId materialId, Autodesk.Revit.DB.Document familyDocument)
		{
			this.m_Revit = cmdData;
			this.m_RevitApp = this.m_Revit.Application.Application;
			this.m_FamilyDocument = familyDocument;
			this.m_MaterialId = materialId;
			this.m_FamilyItemFactory = this.m_FamilyDocument.FamilyCreate;
			this.GetDocumentTemplateInfos();
		}

		public Result AddReferencePlanes(ref string message)
		{
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result CreateExtrusionForm(RampType rampType, List<Curve> rampSection, double rampLength, double rampWidth, double rampBottomWidth, double rampHeight, XYZ slopeDir, List<XYZ> skidResistancePosition, SkidResistanceType srType, ref string message)
		{
			double num = rampWidth;
			Extrusion extrusion = null;
			if (rampType == RampType.RT_Car && Geometry.GreaterThan(rampBottomWidth, rampWidth))
			{
				num = rampBottomWidth;
				List<Curve> list = new List<Curve>();
				XYZ zero = XYZ.Zero;
				XYZ xyz = new XYZ((rampBottomWidth - rampWidth) / 2.0, 0.0, rampHeight);
				XYZ xyz2 = new XYZ(rampBottomWidth - (rampBottomWidth - rampWidth) / 2.0, 0.0, rampHeight);
				XYZ xyz3 = new XYZ(rampBottomWidth, 0.0, 0.0);
				XYZ xyz4 = new XYZ(rampBottomWidth, 0.0, rampHeight * 2.0);
				XYZ xyz5 = new XYZ(0.0, 0.0, rampHeight * 2.0);
				Line item = YJKLineEx.YJKGetBound(zero, xyz);
				list.Add(item);
				Line item2 = YJKLineEx.YJKGetBound(xyz, xyz2);
				list.Add(item2);
				Line item3 = YJKLineEx.YJKGetBound(xyz2, xyz3);
				list.Add(item3);
				Line item4 = YJKLineEx.YJKGetBound(xyz3, xyz4);
				list.Add(item4);
				Line item5 = YJKLineEx.YJKGetBound(xyz4, xyz5);
				list.Add(item5);
				Line item6 = YJKLineEx.YJKGetBound(xyz5, zero);
				list.Add(item6);
				XYZ origin = new XYZ(0.0, 0.0, 0.0);
				Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(-XYZ.BasisY, origin);
				SketchPlane skplane = RevitVersionFuncs.NewSketchPlane(this.m_FamilyDocument, plane);
				if (!this.CreateExtrusionForm(list, skplane, rampLength, ref extrusion))
				{
					message = "创建坡道失败，截面无法拉伸";
					return Autodesk.Revit.UI.Result.Failed;
				}
			}
			XYZ origin2 = new XYZ(0.0, 0.0, 0.0);
			Plane plane2 = RevitVersionFuncs.CreatePlanByNormalAndOrigin(XYZ.BasisX, origin2);
			SketchPlane skplane2 = RevitVersionFuncs.NewSketchPlane(this.m_FamilyDocument, plane2);
			Extrusion extrusion2 = null;
			if (!this.CreateExtrusionForm(rampSection, skplane2, num, ref extrusion2))
			{
				message = "创建坡道失败，截面无法拉伸";
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (srType == SkidResistanceType.SRT_None || skidResistancePosition.Count == 0)
			{
				if (extrusion != null)
				{
					CombinableElementArray combinableElementArray = new CombinableElementArray();
					combinableElementArray.Append(extrusion2);
					extrusion.get_Parameter(BuiltInParameter.ELEMENT_IS_CUTTING).Set(1);
					combinableElementArray.Append(extrusion);
					this.m_FamilyDocument.CombineElements(combinableElementArray);
				}
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			double num2 = Common.MMToFeet(50.0);
			XYZ origin3 = new XYZ(num2, 0.0, 0.0);
			Plane plane3 = RevitVersionFuncs.CreatePlanByNormalAndOrigin(XYZ.BasisX, origin3);
			SketchPlane skplane3 = RevitVersionFuncs.NewSketchPlane(this.m_FamilyDocument, plane3);
			List<Extrusion> list2 = new List<Extrusion>();
			double num3 = Common.MMToFeet(10.0);
			double rampWidth2 = num - num2 * 2.0;
			XYZ xyz6 = new XYZ(num2, 0.0, 0.0);
			foreach (XYZ xyz7 in skidResistancePosition)
			{
				XYZ xyz8 = xyz7 + xyz6;
				List<Curve> list3 = new List<Curve>();
				XYZ xyz9;
				XYZ radianPnt;
				XYZ xyz10;
				if (srType == SkidResistanceType.SRT_Concave)
				{
					if (Geometry.LessThan(xyz7.Z - num3, 0.0))
					{
						continue;
					}
					xyz9 = xyz8 - slopeDir * num3;
					radianPnt = xyz8 - XYZ.BasisZ * num3;
					xyz10 = xyz8 + slopeDir * num3;
				}
				else
				{
					if (srType != SkidResistanceType.SRT_Protrude)
					{
						continue;
					}
					xyz9 = xyz8 + slopeDir * num3;
					radianPnt = xyz8 + XYZ.BasisZ * num3;
					xyz10 = xyz8 - slopeDir * num3;
				}
				Arc item7 = this.m_Revit.Application.Application.CreatYJKArc(xyz9, xyz10, radianPnt);
				list3.Add(item7);
				Line item8 = YJKLineEx.YJKGetBound(xyz10, xyz9);
				list3.Add(item8);
				Extrusion item9 = null;
				try
				{
					if (this.CreateExtrusionForm(list3, skplane3, rampWidth2, ref item9))
					{
						list2.Add(item9);
					}
				}
				catch (Exception)
				{
				}
			}
			if (srType == SkidResistanceType.SRT_Protrude)
			{
				CombinableElementArray combinableElementArray2 = new CombinableElementArray();
				foreach (Extrusion extrusion3 in list2)
				{
					combinableElementArray2.Append(extrusion3);
				}
				combinableElementArray2.Append(extrusion2);
				if (extrusion != null)
				{
					extrusion.get_Parameter(BuiltInParameter.ELEMENT_IS_CUTTING).Set(1);
					combinableElementArray2.Append(extrusion);
				}
				this.m_FamilyDocument.CombineElements(combinableElementArray2);
			}
			else if (srType == SkidResistanceType.SRT_Concave)
			{
				CombinableElementArray combinableElementArray3 = new CombinableElementArray();
				foreach (Extrusion extrusion4 in list2)
				{
					extrusion4.get_Parameter(BuiltInParameter.ELEMENT_IS_CUTTING).Set(1);
					combinableElementArray3.Append(extrusion4);
				}
				if (extrusion != null)
				{
					extrusion.get_Parameter(BuiltInParameter.ELEMENT_IS_CUTTING).Set(1);
					combinableElementArray3.Append(extrusion);
				}
				combinableElementArray3.Append(extrusion2);
				this.m_FamilyDocument.CombineElements(combinableElementArray3);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private bool CreateExtrusionForm(List<Curve> rampSection, SketchPlane skplane, double rampWidth, ref Extrusion extrusion)
		{
			CurveArray curveArray = this.CreateProfileShape(rampSection);
			CurveArrArray curveArrArray = new CurveArrArray();
			curveArrArray.Append(curveArray);
			bool flag = true;
			try
			{
				extrusion = this.m_FamilyDocument.FamilyCreate.NewExtrusion(flag, curveArrArray, skplane, rampWidth);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				return false;
			}
			return extrusion != null;
		}

		private CurveArray CreateProfileShape(List<Curve> rampSection)
		{
			CurveArray curveArray = new CurveArray();
			foreach (Curve curve in rampSection)
			{
				curveArray.Append(curve);
			}
			return curveArray;
		}

		public Result AddAlignments(ref string message)
		{
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result AddParameters(ref string message)
		{
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result AddDimensions(ref string message)
		{
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result AddSymbols(ref string symbolName, ref string message)
		{
			try
			{
				FamilyManager familyManager = this.m_FamilyDocument.FamilyManager;
				symbolName = "标准";
				FamilyType currentType = familyManager.NewType(symbolName);
				familyManager.CurrentType = currentType;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		protected Result GetDocumentTemplateInfos()
		{
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		protected ExternalCommandData m_Revit;

        protected Autodesk.Revit.DB.Document m_FamilyDocument;

		protected ElementId m_MaterialId;

		protected Autodesk.Revit.ApplicationServices.Application m_RevitApp;

		protected FamilyItemFactory m_FamilyItemFactory;
	}
}
